﻿using System;
using System.Collections.Generic;

using System.Text;
using NHibernate;
using OpenGado.DATA.Entities;

namespace OpenGado.DATA
{
    public class CategoryDataControl
    {

        private static CategoryDataControl categoryDataControl;
        private static object lockCategoryControl = new object();

        private ISession session;
        /// <summary>
        /// Return a valid session 
        /// </summary>
        public ISession Session
        {

           
            get
            {
                session = FactoryHandler.CreateFactory().OpenSession();
                return session;
            }

        }
        /// <summary>
        /// Allows are to access the class without creating instance of it first, this is done in the get accessor.
        /// </summary>
        public static CategoryDataControl Instance
        {
            get
            {
                lock(lockCategoryControl)
                    if (categoryDataControl ==null)
                    {
                        categoryDataControl = new CategoryDataControl();
                    }

                return categoryDataControl;
            }
        }

        /// <summary>
        /// Saves the category to the db.
        /// </summary>
        /// <param name="category"></param>
        public void Save(Categories category)
        {

            ITransaction transaction = null;
            ISession tsession = Session; 
            transaction = tsession.BeginTransaction();

            try
            {
                session.SaveOrUpdate(category);
                transaction.Commit();
               
            }
            catch (Exception)
            {
               //TODO: Add loging on execption. 

                transaction.Rollback();
            }
            finally
            {
                tsession.Close();
            }
        }

        /// <summary>
        /// Deletes the category from the db.
        /// </summary>
        /// <param name="category"></param>
        public void Delete (Categories category)
        {  
            
            ITransaction transaction = null;
            ISession tsession = Session; 
            try
            {
              
                transaction = tsession.BeginTransaction();
                this.session.Delete(category);
                transaction.Commit();
               
            }
            catch (Exception)
            {
                if (transaction != null) transaction.Rollback();
                throw;
            }
            finally
            {
                tsession.Close();
            }
        }

        /// <summary>
        /// Returns a list of all categories
        /// </summary>
        /// <returns></returns>
        public IList<Categories> GetAllCategories ()
        {
            ISession tsession = Session;

            ICriteria criteria = tsession.CreateCriteria<Categories>();
            IList<Categories> categories = criteria.List<Categories>();

            tsession.Close();

            return categories;
        }
        /// <summary>
        /// Gets a category by its ID
        /// </summary>
        /// <returns></returns>
        public  Categories GetCategoryById(int categoryId)
        {
            Categories cat = new Categories();

            ISession tsession = Session;
            try
            {
                cat = tsession.Get<Categories>(categoryId);
            }
            catch (Exception)
            {
                
                
            }
            finally
            {
                tsession.Close();
            }
            return cat;
        }
    }
}
